{% extends 'layout/manage.html' %}
{% load static %}
{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.preview.css' %}">
    <style>
        .panel-default {
            margin-top: 10px;
        }

        .panel-body {
            padding: 0;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .title-list {
            border-right: 1px solid #ddd;
            min-height: 500px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .content {
            border-left: 1px solid #ddd;
            min-height: 600px;
            margin-left: -1px;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div>
                    <a href=""><i class="fa fa-book" aria-hidden="true"></i> wiki文档</a>
                </div>
                <div class="function">
                    <a href="{% url 'wiki_add' request.tracer.project.id %}" class="btn btn-primary btn-xs"><i
                            class="fa fa-plus-circle" aria-hidden="true"></i> 新建</a>
                    {% if wiki_obj %}
                        <a href="{% url 'wiki_edit' project_id=request.tracer.project.id  wiki_id=wiki_obj.id %}"
                           class="btn btn-danger btn-xs"><i
                                class="fa fa-edit" aria-hidden="true"></i> 编辑</a>
                        <a href="{% url 'wiki_delete' project_id=request.tracer.project.id wiki_id=wiki_obj.id %}"
                           class="btn btn-danger btn-xs"><i
                                class="fa fa-trash" aria-hidden="true"></i> 删除</a>
                    {% endif %}
                </div>
            </div>
            <div class="panel-body">
                <div class="col-sm-3 title-list">
                    <ul id="catalog">

                    </ul>
                </div>
                <div class="col-sm-9 content">
                    {% if wiki_obj %}
                        <div id="test-markdown-view">
                            <!-- Server-side output Markdown text -->
                            <textarea style="display:none;">{{ wiki_obj.content }}</textarea>
                        </div>
                    {% else %}
                        <div class="text-center" style="margin-top: 50px;">
                            <h3>《{{ request.tracer.project.name }}》wiki文档库</h3>
                            <a href="{% url 'wiki_add' request.tracer.project.id %}"><i class="fa fa-plus-circle"
                                                                                        aria-hidden="true"></i> 新建文章</a>
                        </div>
                    {% endif %}

                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script src="{% static 'js/jquery-3.3.1.js' %}"></script>
    <script src="{% static 'plugin/editor-md/editormd.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/prettify.min.js' %}"></script>

    <script>
        var WIKi_DETAIL_URL = "{% url 'wiki' request.tracer.project.id %}";
        $(function () {
            init_catalog();
        })

        $(function () {
            var testView = editormd.markdownToHTML("test-markdown-view", {
                // markdown : "[TOC]\n### Hello world!\n## Heading 2", // Also, you can dynamic set Markdown text
                // htmlDecode : true,  // Enable / disable HTML tag encode.
                htmlDecode : "style,script,iframe",  // Note: If enabled, you should filter some dangerous HTML tags for website security.
            });
        });

        function init_catalog() {
            $.ajax({
                url: "{% url 'init_catalog' request.tracer.project.id %}",
                type: 'GET',
                dataType: "JSON",
                success: function (res) {
                    if (res.status) {
                        console.log(res.data)
                        $.each(res.data, function (index, item) {
                            var href = WIKi_DETAIL_URL + "?wiki_id=" + item.id;
                            var li = $("<li>").attr("id", "id_" + item.id).append($("<a>").attr("href", href).text(item.title)).append($("<ul>"));
                            if (!item.parent_id) {
                                $("#catalog").append(li);
                            } else {
                                $("#id_" + item.parent_id).children("ul").append(li)
                            }
                        });
                    } else {
                        alert("初始化目录失败")
                    }
                }
            });
        }
    </script>
{% endblock %}